package fr.emse.tatiana.replayable.analysis.graph;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.actions.ActionFactory;

public class DeleteAction extends SelectionAction {

	public DeleteAction(IWorkbenchPart part) {
		super(part);
		//setLazyEnablementCalculation(false);
	}

	@Override
	protected void init() {
		setText("Delete...");
		setToolTipText("Delete");
		setId(ActionFactory.DELETE.getId());
		setEnabled(false);
	}
	
	@Override
	protected boolean calculateEnabled() {
		Command cmd = createDeleteCommand();
		if (cmd == null)
			return false;
		return true;
	}

	public Command createDeleteCommand() {
		Request request = new Request("delete");
		EditPart part = null;
		try {
			part = (EditPart) getSelectedObjects().get(0);
		} catch (Exception e) {
			return null; //TODO figure out why this action is applied in the error log and with no object selected
		}
		return part.getCommand(request);
	}

	@Override
	public void run() {
		execute(createDeleteCommand());
	}
	
}
